#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: ice_helpers.py
Date: January 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

================================================================================
Module Description:

Helpers for the Ice system.

"""

#-------------------------------------------------------------------------------
# System imports
import sys
import Ice, IceStorm
from time import sleep

# Import the Ice interface definitions
import AcornRxUpdate
import AcornRxDsp
import AcornRxControl
import AcornIqIn
import AcornRxOut
import AcornPersist

#-------------------------------------------------------------------------------
def get_interface(caller, communicator, proxy_object, proxy_string):

    for n in range(10):
        try:
            interface = proxy_object.checkedCast(communicator().propertyToProxy(proxy_string))
            if interface:
                return interface
        except:
            sleep(1)
    
    if not interface:
        print 'Acorn (', caller, ') : invalid proxy'
        sys.exit(1)
        
#-------------------------------------------------------------------------------
def get_topic_manager(caller, communicator):
    
    manager = IceStorm.TopicManagerPrx.checkedCast(communicator().propertyToProxy('TopicManager.Proxy'))
    if not manager:
        print 'Acorn (', caller, ') : invalid topic manager proxy'
        sys.exit(1)
    
    return manager

#-------------------------------------------------------------------------------
def get_topic(caller, manager, topic_name):
    
    try:
        topic = manager.retrieve(topic_name)
    except IceStorm.NoSuchTopic, e:
        try:
            topic = manager.create(topic_name)
        except IceStorm.TopicExists, ex:
            print 'Acorn (', caller, ') : temporary error getting topic, try again'
            sys.exit(1)
            
    return topic


#-------------------------------------------------------------------------------
def subscribe(caller, subscriber_topic, subscriber, id):

    # Set up proxy for subscriber
    qos = {}
    try:
        subscriber_topic.subscribeAndGetPublisher(qos, subscriber)
    except IceStorm.AlreadySubscribed, ex:
        # If we're manually setting the subscriber id ignore.
        if len(id) == 0:
            raise
        print "reactivating persistent subscriber"